package net.melon.cata.inst.struts2;

import java.util.Collection;
import java.util.Collections;
import java.util.List;

import javax.inject.Inject;

import net.esj.basic.core.engine.util.tree.TreeConverter;
import net.esj.basic.exception.MelonException;
import net.esj.basic.plugins.view.FacadeAware;
import net.esj.basic.view.PageAction;
import net.melon.cata.CatalogRegistry;
import net.melon.cata.manager.CatalogManager;
import net.melon.cata.manager.CatalogService;
import net.melon.cata.manager.exception.OverMaxCountException;
import net.melon.cata.pojo.Catalog;
import net.melon.cata.pojo.CatalogVo;

@SuppressWarnings("unused")
public class  CatalogAction extends PageAction<CatalogVo, CatalogVo> implements FacadeAware {

	/**
	 * 
	 */
	private static final long serialVersionUID = -322940917096544135L;

	private CatalogManager cataManager;
	
	private CatalogVo parent;
	
	
	private String template;
	private String theme;
	private String namespace;
	
	private String key;
	
	private TreeConverter treeConverter;
	
	@Override
	public void datagrid() {
		List list = Collections.EMPTY_LIST;
		if(id!=null){
			list= cataManager.getChildren(Long.valueOf(id),entityClass());
		}else{
			list = cataManager.getChildren(id,entityClass());
			
		}
		Collection values = treeConverter.convert(list);
		writeJson(values);
	}
	
	@Override
	public String edit() {
		entity =  cataManager.get(Long.valueOf(id),entityClass());
		return SUCCESS;
	}

	@Override
	public void doAdd() throws MelonException, OverMaxCountException {
		cataManager.save(entity,entityClass());
	}

	@Override
	public void doUpdate() {
		cataManager.update(entity,entityClass());
	}

	@Override
	protected void doDelete() throws Exception {
		cataManager.delete(new CatalogVo(Long.valueOf(id)),entityClass());
	}

	public String select(){
		return SUCCESS;
	}
	
	public CatalogVo getEntity(){
		return entity;
	}
	 
	public void setEntity(CatalogVo entity){
		this.entity = entity;
	}

	public CatalogVo getParent() {
		return parent;
	}

	public void setParent(CatalogVo parent) {
		this.parent = parent;
	}

	public String getTemplate() {
		return template;
	}

	@Inject
	public void setCataManager(CatalogManager cataManager) {
		this.cataManager = cataManager;
	}

	public void setTemplate(String template) {
		this.template = template;
	}

	public String getTheme() {
		return theme;
	}

	public void setTheme(String theme) {
		this.theme = theme;
	}

	public String getNamespace() {
		return namespace;
	}

	public void setNamespace(String namespace) {
		this.namespace = namespace;
	}

	@Inject
	public void setTreeConverter(TreeConverter treeConverter) {
		this.treeConverter = treeConverter;
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	private Class entityClass(){
		return CatalogRegistry.getEntityClass(key);
	}

}
